﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastColoredTextBoxNS;
using System.Text.RegularExpressions;
namespace VASMide
{
    public partial class codeEditor : Form
    {
        public codeEditor()
        {
            InitializeComponent();
        }
        public void setLanguageCOBALT()
        {
            keywords.Add("void");
            keywords.Add("string");
            keywords.Add("int");
            keywords.Add("if");
            keywords.Add("for");
            keywords.Add("goto");
            keywords.Add("using");
            keywords.Add("namespace"); 
            keywords.Add("static");
            keywords.Add("class");
            keywords.Add("byte"); keywords.Add("new");
           
        }
        public void setLanguageVASM()
        {
            keywords.Add("push");
            keywords.Add("pop");
            keywords.Add("add");
            keywords.Add("sub");
            keywords.Add("mul");
            keywords.Add("div");
            keywords.Add("call");
            keywords.Add("jmp");
            keywords.Add("halt");
            keywords.Add("bre");
            keywords.Add("bne");
            keywords.Add("blt");
            keywords.Add("bgt");
            keywords.Add("proc");
            keywords.Add("ret");
            sysf.Add("string");
            sysf.Add("dword");
        }
        public Style stringStyle = new TextStyle(Brushes.Blue, null, FontStyle.Regular);
        public Style commentStyle = new TextStyle(Brushes.Gray, null, FontStyle.Regular);
        public Style keywordStyle = new TextStyle(Brushes.DarkMagenta, null, FontStyle.Bold);
        Style cyanStyle = new TextStyle(Brushes.DarkCyan, null, FontStyle.Regular);
        public List<string> indentWords = new List<string>();
        public List<string> unindentWords = new List<string>();
        public List<string> keywords = new List<string>(); List<string> sysf = new List<string>();
        private void fastColoredTextBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            //new edition
            Range range = (sender as FastColoredTextBox).Range;

            range.ClearStyle(stringStyle);
            range.ClearStyle(keywordStyle);
            range.ClearStyle(commentStyle);
            range.SetStyle(stringStyle, @"('.*?\')|(.*\')", RegexOptions.Singleline |
            RegexOptions.RightToLeft);
            range.SetStyle(stringStyle, @"(" + "\"" + @".*?\" + "\"" + @")|(.*\" + "\"" + ")", RegexOptions.Singleline |
            RegexOptions.RightToLeft);
            range.SetStyle(commentStyle, @"#.*$", RegexOptions.Multiline);
            range.SetStyle(cyanStyle, @"sysf_.*$", RegexOptions.RightToLeft);
            foreach (string i in keywords)
            {
                // range.SetStyle(keywordStyle, i + " ", RegexOptions.Singleline);
                range.SetStyle(keywordStyle,  i , RegexOptions.Singleline);
            }
            foreach (string i in sysf)
            {
                range.SetStyle(cyanStyle, i, RegexOptions.Multiline);

            }
        }

        private void codeEditor_Load(object sender, EventArgs e)
        {
            stringStyle = new TextStyle(new SolidBrush(VASMide.Properties.Settings.Default.stringColor), null, FontStyle.Regular);
            commentStyle = new TextStyle(new SolidBrush(VASMide.Properties.Settings.Default.comment), null, FontStyle.Regular);
            keywordStyle = new TextStyle(new SolidBrush(VASMide.Properties.Settings.Default.keyWord), null, FontStyle.Bold);
        }
        public void RefreshHighlight()
        {
            fastColoredTextBox1_TextChanged(this, null);
        }

        private void fastColoredTextBox1_AutoIndentNeeded(object sender, AutoIndentEventArgs e)
        {
            foreach (string str in indentWords)
            {
                if (e.LineText.Trim().StartsWith(str))
                {
                    e.ShiftNextLines = e.TabLength;
                    return;
                }
            }
            if (e.LineText.Trim().EndsWith(":")) 
            {
                e.Shift = -e.TabLength;
            }
            foreach (string str in unindentWords)
            {
                if (e.LineText.Trim().StartsWith(str))
                {
                    e.Shift = -e.TabLength;
                    e.ShiftNextLines = -e.TabLength;
                    return;
                }
            }
        }

        private void fastColoredTextBox1_Load(object sender, EventArgs e)
        {

        }
    }
}
